package com.zfx.service.impl;

import com.zfx.mapper.ISecondarySystemPeopleMessageMapper;
import com.zfx.pojo.SecondarySystemPeopleMessage;
import com.zfx.service.ISecondarySystemPeopleMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: zfx
 * @version: 1.0
 * @time: 2020-05-13 20:51
 * <p>
 * 修改历史：
 * 修改日期      修改人员    版本    内容
 * 2020-05-13   zfx       1.0
 */
@Service
public class SecondarySystemPeopleMessageServiceImpl implements ISecondarySystemPeopleMessageService {

    @Autowired
    private ISecondarySystemPeopleMessageMapper secondarySystemPeopleMessageMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(List<SecondarySystemPeopleMessage> secondarySystemPeopleMessageList) {

        secondarySystemPeopleMessageMapper.deleteByLevel1SystemIdAndTypeId(
                secondarySystemPeopleMessageList.get(0).getLevel1SystemId(),
                secondarySystemPeopleMessageList.get(0).getTypeId()
        );

        int num = 0;
        int secondarySystemId = Integer.valueOf(secondarySystemPeopleMessageMapper.findSecondarySystemIdByLevel1SystemIdAndTypeId(
                secondarySystemPeopleMessageList.get(0).getLevel1SystemId(),
                secondarySystemPeopleMessageList.get(0).getTypeId()
        ));
        for ( SecondarySystemPeopleMessage systemPeopleMessage : secondarySystemPeopleMessageList) {
            systemPeopleMessage.setSecondarySystemId(secondarySystemId);
            num += secondarySystemPeopleMessageMapper.insert(systemPeopleMessage);
        }

        return num;
    }

    @Transactional(readOnly = true)
    @Override
    public List<SecondarySystemPeopleMessage> selectByLevel1SystemIdAndTypeId(int level1SystemId, int typeId) {
        return secondarySystemPeopleMessageMapper.findByLevel1SystemIdAndTypeId(level1SystemId, typeId);
    }

    @Override
    public int deleteByTableId(int level1SystemId) {


        secondarySystemPeopleMessageMapper.deleteByLevel1SystemIdAndTypeId(level1SystemId, 1);
        secondarySystemPeopleMessageMapper.deleteByLevel1SystemIdAndTypeId(level1SystemId, 2);
        secondarySystemPeopleMessageMapper.deleteByLevel1SystemIdAndTypeId(level1SystemId, 3);
        secondarySystemPeopleMessageMapper.deleteByLevel1SystemIdAndTypeId(level1SystemId, 41);
        secondarySystemPeopleMessageMapper.deleteByLevel1SystemIdAndTypeId(level1SystemId, 42);
        secondarySystemPeopleMessageMapper.deleteByLevel1SystemIdAndTypeId(level1SystemId, 43);
        secondarySystemPeopleMessageMapper.deleteByLevel1SystemIdAndTypeId(level1SystemId, 44);
        secondarySystemPeopleMessageMapper.deleteByLevel1SystemIdAndTypeId(level1SystemId, 45);
        return 0;
    }


}
